import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent {
  myForm: FormGroup;

  constructor(private fb: FormBuilder, private router: Router, private http: HttpClient) {
    this.myForm = this.fb.group({
      username: ['', Validators.required],
      password: ['', [Validators.required, Validators.minLength(6)]]
    });
  }

  onSubmit() {
    console.log('Form submitted');
    this.http.post('http://localhost:8087/login', this.myForm.value).subscribe((response: any) => {
      console.log('提交成功', response);
      if (response.status === 'success') {
        this.router.navigate(['/dashboard']);
      }
      
    }, error => {
      console.error('提交失败', error);
    });
  }
}
